/**
 * Class regression
 * 
 * @author Hussein Patwa
 * @date 10 April 2008 - Rev 07 May 2008
 * @version 3.0 REgression functions for use in local and global
 *          trending algorithms
 */
package trend;

import java.util.*;

@SuppressWarnings("all")
public class Regression {
  /**
   * Constructor for regression
   * 
   * @param predictions Collection of predicted observations
   */

  TreeMap predictions;

  /**
   * Calculate the regression
   * 
   * @param pathObs paths of observations
   */
  public Regression(TreeMap pathObs) {
    predictions = new TreeMap();
    double sigmaX = Utils.sigmaX(pathObs.keySet());
    double sigmaX2 = Utils.sigmaXSquare(pathObs.keySet());
    double sigmaY = Utils.sigmaY(pathObs.values());
    double sigmaXY = Utils.sigmaXY(pathObs);
    double slope = Utils.getLeastSquareSlope(sigmaX, sigmaX2, sigmaY,
        sigmaXY, pathObs.size());// compute the slope with sign
    double constantC = Utils.getLeastSquareC(sigmaX, sigmaX2, sigmaY,
        sigmaXY, pathObs.size());
    for (Iterator i = pathObs.entrySet().iterator(); i.hasNext();) {
      Map.Entry e = (Map.Entry) i.next();
      Double key = (Double) e.getKey();
      double predictedVal = slope * key.doubleValue() + constantC;
      predictions.put(key, new Double(predictedVal));
    }
  }

  /**
   * Print the list of regression predictions
   * 
   * @return TreeMap
   */
  TreeMap getRegressionPredictions() {
    return predictions;
  }

}
